<?php
/**
 * Object Relational Persistence Mapping Class for table: pha_req_exticket
 *
 * @package	modules.dept_pharmacy.ordo
 * @author	viethd
 * @version $
 * @copyright 2009 LHV Company
 */
class PhaReqExticket extends ORDataObject {

    /**#@+
     * Fields of table: pha_req_exticket mapped to class members
     */
    var $pha_req_exticket_id		= '';
    var $req_exticket_code		= '';
    var $req_date		= '';
    var $source_id		= '';
    var $source_user_id		= '';
    var $enum_source_type		= '';
    var $enum_request_type		= '';
    var $enum_item_type		= '';
    var $enum_status		= '';
    var $enum_destination		= '';
    /**#@-*/


    /**
     * DB Table
     */
    var $_table = 'pha_req_exticket';

    /**
     * Primary Key
     */
    var $_key = 'pha_req_exticket_id';

    /**
     * Internal Name
     */
    var $_internalName = 'PhaReqExticket';

    /**
     * Handle instantiation
     */
    function __construct() {
        parent::ORDataObject();
    }

    public function setStatus($phaReqExticketId, $status) {
    
        $sql = "UPDATE $this->_table SET enum_status = " . $status .
                " WHERE pha_req_exticket_id='$phaReqExticketId'";
        $this->_execute($sql);      

    }



    public function listPendingRequestFromDept($filter) {

        $field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');       
        $condition = true;
        if (isset($filter['enum_destination'])) {
            $condition .= ' AND enum_destination=' . $filter['enum_destination'] ;
        }

        $ds =& new Datasource_sql();
        $sql = array(
               'cols'  => "pha_req_exticket_id, req_exticket_code,
                req_date, source_id, d.$field source_name , source_user_id, p.name source_user_name,
                enum_request_type, enum_item_type, enum_destination",
               'from'  => "$this->_table t INNER JOIN department d ON (t.source_id=d.department_id
                            AND t.enum_source_type='" . EnumerationConstant::PHA_REQ_SOURCE_TYPE_DEPT_KEY . "'
                            AND t.enum_status='" . EnumerationConstant::PHA_REQ_STATUS_PENDING_KEY  . "')                               
                           LEFT JOIN person p on (source_user_id=p.person_id)                               
                           ",
                'where' => $condition               
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    }

    public function listProcessedRequestFromDrugStore($filter = array()) {

         
    }
    public function listProcessedRequestFromDept($filter = array()) {
        $field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $ds =& new Datasource_sql();
        $condition = true;
        if (isset($filter['enum_destination'])) {
            $condition .= '  AND enum_destination=' . $filter['enum_destination'] ;
        }

        $sql = array(
               'cols'  => "t.pha_req_exticket_id, req_exticket_code,
                req_date, source_id, d.$field source_name, source_user_id, p.name source_user_name,
                    enum_request_type, enum_item_type, enum_destination,
                    app_user_id, pp.name app_user_name, app_date, t.enum_status",
               'from'  => "$this->_table t INNER JOIN department d ON (t.source_id=d.department_id
                            AND t.enum_source_type='" . EnumerationConstant::PHA_REQ_SOURCE_TYPE_DEPT_KEY . "'
                               AND t.enum_status IN('" . EnumerationConstant::PHA_REQ_STATUS_ACCEPTED_KEY  . "',
                               '" . EnumerationConstant::PHA_REQ_STATUS_REJECTED_KEY  . "'))
                           LEFT JOIN person p on (source_user_id=p.person_id)                                
                           INNER JOIN pha_ewh_exticket pee ON (t.pha_req_exticket_id=pee.pha_req_exticket_id)
                           LEFT JOIN person pp on (app_user_id=pp.person_id) ",
                'where' => $condition               
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;

    }

    public function listPendingRequests($filter) {


        $condition = true;
        if (isset($filter['enum_destination'])) {
            $condition .= ' AND t.enum_destination=' . $filter['enum_destination'] ;
        }

        $ds =& new Datasource_sql();
        $sql = array(
               'cols'  => "t.pha_req_exticket_id, t.req_exticket_code,
                t.req_date, t.source_id, t.source_user_id, p.name source_user_name,
                t.enum_request_type, t.enum_item_type, t.enum_destination,t.enum_source_type",
               'from'  => "$this->_table t INNER JOIN $this->_table t1 ON (t.pha_req_exticket_id=t1.pha_req_exticket_id                            
                            AND t.enum_status='" . EnumerationConstant::PHA_REQ_STATUS_PENDING_KEY  . "')                               
                           LEFT JOIN person p ON (t.source_user_id=p.person_id)",
                'where' => $condition               
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;

    }
    private function buildProcessedRequestsCondition($filterData = array()) {
        
        $sql = true;
             
        if (isset($filterData['enum_destination'])) {
            $sql .= ' AND t.enum_destination=' . $filterData['enum_destination'] ;
        }
        
        if (isset($filterData['reqTicketCodeFilter']) &&  trim($filterData['reqTicketCodeFilter']) !== "") {
            $sql = $sql. " AND (t.req_exticket_code LIKE '%".trim($filterData['reqTicketCodeFilter'])."%')";
        }
        if (isset($filterData['fromDate']) &&  $filterData['fromDate'] !== "") {
            $sql = $sql. " AND t.req_date >= '" . $filterData['fromDate'] . "'";
        }
        if (isset($filterData['toDate']) &&  $filterData['toDate'] !== "") {
            $sql = $sql. " AND t.req_date <= '" . $filterData['toDate'] . "'";
        }        
        return $sql;       
    }

    public function listProcessedRequests($filter = array()) {
               
        
        $ds =& new Datasource_sql();
        $sql = array(
               'cols'  => "t.pha_req_exticket_id, t.req_exticket_code,
                t.req_date, t.source_id, t.source_user_id, p.name source_user_name,
                t.enum_request_type, t.enum_item_type, t.enum_destination,t.enum_source_type,
                app_user_id, pp.name app_user_name, app_date, t.enum_status",
               'from'  => "$this->_table t INNER JOIN $this->_table t1 ON (t.pha_req_exticket_id=t1.pha_req_exticket_id                            
                            AND t.enum_status IN('" . EnumerationConstant::PHA_REQ_STATUS_ACCEPTED_KEY  . "',
                               '" . EnumerationConstant::PHA_REQ_STATUS_REJECTED_KEY  . "'))
                           LEFT JOIN pha_ewh_exticket pee ON (t.pha_req_exticket_id=pee.pha_req_exticket_id)
                           LEFT JOIN person p ON (t.source_user_id=p.person_id)
                           LEFT JOIN person pp on (app_user_id=pp.person_id) ",
                'where' => $this->buildProcessedRequestsCondition($filter)               
        );
        
        if (isset($filter['limit'])) {
            $sql['limit'] = $filter['limit'];
            
        }
        
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;

    }
    
    public function getRequest($reqId, $filter = array()) {

        $condition = true;
        if (isset($filter['enum_destination'])) {
            $condition .= ' AND t.enum_destination=' . $filter['enum_destination'] ;
        }

        $ds =& new Datasource_sql();
        $sql = array(
               'cols'  => "t.pha_req_exticket_id, t.req_exticket_code,
                t.req_date, t.source_id, t.source_user_id, p.name source_user_name,
                t.enum_request_type, t.enum_item_type, t.enum_destination,t.enum_source_type,
                app_user_id, pp.name app_user_name, app_date, t.enum_status",
               'from'  => "$this->_table t INNER JOIN $this->_table t1 ON (t.pha_req_exticket_id=t1.pha_req_exticket_id AND t.pha_req_exticket_id='$reqId')
                           LEFT JOIN pha_ewh_exticket pee ON (t.pha_req_exticket_id=pee.pha_req_exticket_id)
                           LEFT JOIN person p ON (t.source_user_id=p.person_id)
                           LEFT JOIN person pp on (app_user_id=pp.person_id) ",
                'where' => $condition               
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return null;
        }
        return $result[0];

    }

}
?>
